package org.td.engine.map;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import org.td.engine.Path;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class GameMap {
    
    public final static int TILE_SIZE = 32;
    
    private int width;
    private int height;
    
    private Texture texture;
    
    private int[][] tiles;
    private boolean[][] pathing;
    
    private List<Path> pathList;
    private MapCamera mapCamera;
    
    public GameMap(int width, int height, MapCamera mapCamera) {
        this.width = width;
        this.height = height;
        this.mapCamera = mapCamera;
        texture = new Texture(Gdx.files.internal("tiles.png"));
        tiles = new int[width][height];
        Random r = new Random();
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                tiles[x][y] = r.nextInt(256);
            }
        }

        pathing = new boolean[width][height];
        pathList = new ArrayList<Path>();
        pathList.add(new Path());
    }
    
    public void setPathing(int tileX, int tileY, boolean pathing) {
        if (!inBounds(tileX, tileY)) {
            return;
        }
        this.pathing[tileX][tileY] = pathing;
    }

    public int getPathing(int tileX, int tileY) {
        if (!inBounds(tileX, tileY)) {
            return 1;
        }
        return pathing[tileX][tileY] ? 1 : 0;
    }
    
    public Path getPath(int i) {
        return pathList.get(i);
    }
    
    public int getPathCount() {
        return pathList.size();
    }

    private boolean inBounds(int tileX, int tileY) {
        if (tileX < 0 || tileY < 0 || tileX >= width || tileY >= height) {
            return false;
        } else {
            return true;
        }

    }
    
    public void render(SpriteBatch batch) {

        int offsX = mapCamera.getX() % TILE_SIZE;
        int offsY = mapCamera.getY() % TILE_SIZE;
        int startX = mapCamera.getX() / TILE_SIZE;
        int startY = mapCamera.getY() / TILE_SIZE;
        int cameraTileWidth = mapCamera.getWidth() / TILE_SIZE;
        int cameraTileHeight = mapCamera.getHeight() / TILE_SIZE;
        int tilesX = Math.min(cameraTileWidth + 1, width - startX);
        int tilesY = Math.min(cameraTileHeight + 2, height - startY);

        for (int y = 0; y < tilesY; y++) {
            for (int x = 0; x < tilesX; x++) {
                int id = tiles[startX + x][startY + y];
                int texX = (id % 16) * 32;
                int texY = (id / 16) * 32;
                batch.draw(texture, -offsX + x * TILE_SIZE, -offsY + y * TILE_SIZE, texX, texY, 32, 32);

            }
        }

        /*for (Path p : pathList) {
            p.debugRender(TILE_SIZE, TILE_SIZE);
        }*/

    }
    
    public static int absoluteX(int tileX) {
        return tileX * TILE_SIZE;
    }
    
    public static int absoluteY(int tileY) {
        return tileY * TILE_SIZE;
    }

    public int getTileWidth() {
        return TILE_SIZE;
    }

    public int getTileHeight() {
        return TILE_SIZE;
    }

    
}
